package com.cloud.controller;

import com.cloud.mall.order.TbOrder;
import com.cloud.mall.order.dto.OrderDto;
import com.cloud.service.ITbOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器 GUN
 * </p>
 *
 * @author jobob
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/order/tb-order")
public class TbOrderController {
    @Autowired
    private ITbOrderService iTbOrderService;

    @RequestMapping("/getById")
    public TbOrder getById(@RequestParam("id") Long id) {
        if (null == id) {
            return null;
        }
        return iTbOrderService.getById(id);
    }

    @RequestMapping("/save")
    public boolean save(@RequestBody TbOrder tbOrder) {
        if (null == tbOrder) {
            throw new RuntimeException("数据为空，插入失败");
        }
        return iTbOrderService.save(tbOrder);
    }

    @RequestMapping("/delete")
    public boolean delete(@RequestParam("id") Long id) {
        if (null == id) {
            throw new RuntimeException("数据为空，删除失败");
        }
        return iTbOrderService.removeById(id);
    }

    @RequestMapping("/update")
    public boolean update(@RequestBody TbOrder tbOrder) {
        if (null == tbOrder) {
            throw new RuntimeException("数据为空，修改失败");
        }
        return iTbOrderService.updateById(tbOrder);
    }

    @RequestMapping("/listByPage")
    public PageInfo<TbOrder> listByPage(@RequestBody TbOrder tbOrder) {
        int pageNum = 1;
        int pageSize = 10;
        if (null != tbOrder && tbOrder.getPageNum() != null) {
            pageNum = tbOrder.getPageNum();
        }
        if (null != tbOrder && tbOrder.getPageSize() != null) {
            pageSize = tbOrder.getPageSize();
        }
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<TbOrder>(iTbOrderService.list());
    }

    @RequestMapping("/listAll")
    public List<TbOrder> listAll() {
        return iTbOrderService.list();
    }

    @RequestMapping("/order")
    public String order(@RequestBody OrderDto orderDto) {
        return this.iTbOrderService.order(orderDto);
    }
}
